package amap.com.android_path_record;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import amap.com.android_path_record.User.User;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtUserName;
    private EditText mEtPassWord;
    private Button mBtnRegister;
    private Button mBtnLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(this, "6db712399660e9b3096f4aa3d17cb7b1");
        login();
        mEtUserName = (EditText) findViewById(R.id.et_user_name);
        mEtPassWord = (EditText) findViewById(R.id.et_user_pwd);
        mBtnLogin = (Button) findViewById(R.id.btn_login);
        mBtnRegister = (Button) findViewById(R.id.btn_register);
        mBtnRegister.setOnClickListener(this);
        mBtnLogin.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String username = mEtUserName.getText().toString();
        String password = mEtPassWord.getText().toString();
        boolean a = username.isEmpty() || password.isEmpty();
        boolean b = username.contains("@") && (username.contains(".") && (password.length() >= 8));
        if (a == true) {
            Toast.makeText(LoginActivity.this, "邮箱和密码不能为空", Toast.LENGTH_SHORT).show();
        } else if (b == false) {
            Toast.makeText(LoginActivity.this, "邮箱和密码格式错误，请重新输入", Toast.LENGTH_SHORT).show();
        } else {
            switch (v.getId()) {
                case R.id.btn_register:
                    BmobUser user = new BmobUser();
                    user.setUsername(username);
                    user.setPassword(password);
                    user.signUp(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser u, BmobException e) {
                            if (e == null) {
                                Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            } else {
                                //loge(e);
                                Toast.makeText(LoginActivity.this, "注册失败,用户已存在或无网络连接", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                    break;
                case R.id.btn_login:
                    BmobUser user1 = new BmobUser();
                    user1.setUsername(username);
                    user1.setPassword(password);
                    user1.login(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser u2, BmobException e) {
                            if (e == null) {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                                startActivity(intent);
                            } else {
                                //loge(e);
                                Toast.makeText(LoginActivity.this, "登录失败,用户名或密码填写错误", Toast.LENGTH_SHORT).show();
                            }

                        }
                    });
                    break;
                default:
                    break;
            }
        }

    }

    public void login(){
        if (BmobUser.isLogin()) {
            User user = BmobUser.getCurrentUser(User.class);
            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(intent);
        } else {
        }
    }
}
